package com.sixth.service.impl;

import com.sixth.bean.MarketKeyword;
import com.sixth.bean.MarketKeywordExample;
import com.sixth.bean.MarketSearchHistory;
import com.sixth.bean.MarketSearchHistoryExample;
import com.sixth.bean.vo.WXSearchHistoryVO;
import com.sixth.bean.vo.WXSearchIndexDefaultKeywordVO;
import com.sixth.bean.vo.WXSearchIndexVO;
import com.sixth.constant.Constant;
import com.sixth.mapper.MarketKeywordMapper;
import com.sixth.mapper.MarketSearchHistoryMapper;
import com.sixth.service.WXSearchService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @since 2022/09/12 15:25
 * @author zhaoxiangyang
 */
@Service
public class WXSearchImpl implements WXSearchService {
    @Autowired
    MarketKeywordMapper keywordMapper;
    @Autowired
    MarketSearchHistoryMapper historyMapper;
    
    @Override
    public WXSearchIndexVO printIndex(int userId) {
        //   未登录则不会有历史关键字；--返回空list
        // 查出全部 默认、热词 数据记录 ，第一条数据作为搜索框默认；
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
        criteria.andDeletedEqualTo(Constant.EXIST);
        criteria.andIsHotEqualTo(true).andIsDefaultEqualTo(true);
        List<MarketKeyword> keywordList = keywordMapper.selectByExample(marketKeywordExample);

        // 查出的market keyword list---->>>>wxsearch index vo.hotkey list
        WXSearchIndexDefaultKeywordVO wxSearchIndexDefaultKeywordVO = new WXSearchIndexDefaultKeywordVO();
        ArrayList<WXSearchIndexDefaultKeywordVO> wxSearchIndexDefaultKeywordVOS = new ArrayList<>();
        try {
            // 搜索框显示第一个
            BeanUtils.copyProperties(wxSearchIndexDefaultKeywordVO,keywordList.get(0));
            // 搜索框下方显示 默认、热词list
            for (MarketKeyword marketKeyword : keywordList) {
                WXSearchIndexDefaultKeywordVO wxSearchIndexDefaultKeywordVO2 = new WXSearchIndexDefaultKeywordVO();
                BeanUtils.copyProperties(wxSearchIndexDefaultKeywordVO2,marketKeyword);
                wxSearchIndexDefaultKeywordVOS.add(wxSearchIndexDefaultKeywordVO2);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        // 根据userId查出搜索历史表的关键字list
        if(userId== -1){
            // 未登录，先返回----空list
            ArrayList<WXSearchHistoryVO> wxSearchHistoryVOS = new ArrayList<>();
            return new WXSearchIndexVO(wxSearchIndexDefaultKeywordVO,wxSearchIndexDefaultKeywordVOS,wxSearchHistoryVOS);
        }
        // 查询条件
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        marketSearchHistoryExample.setDistinct(true);
        // marketKeywordExample.setOrderByClause("id desc");增加时间可以替代id
        marketKeywordExample.setOrderByClause(Constant.SORT_ADD_TIME + Constant.ORDER_DESC);
        MarketSearchHistoryExample.Criteria historyExampleCriteria = marketSearchHistoryExample.createCriteria();
        historyExampleCriteria.andDeletedEqualTo(false);
        historyExampleCriteria.andUserIdEqualTo(userId);

        List<MarketSearchHistory> historyList = historyMapper.selectByExample2(marketSearchHistoryExample);

        //取前10条历史搜索关键字记录
        int size = historyList.size() > Constant.TEN ? Constant.TEN : historyList.size();
        List<MarketSearchHistory> historyList1 = historyList.subList(Constant.ZERO, size);
        ArrayList<WXSearchHistoryVO> wxSearchHistoryVOS = new ArrayList<>();
        // 显示登陆用户的搜索历史关键字list
        for (MarketSearchHistory marketSearchHistory : historyList1) {
            WXSearchHistoryVO wxSearchHistoryVO = new WXSearchHistoryVO();
            try {
                BeanUtils.copyProperties(wxSearchHistoryVO,marketSearchHistory);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            wxSearchHistoryVOS.add(wxSearchHistoryVO);
        }
        // WXSearchIndexVO wxSearchIndexVO = new WXSearchIndexVO();
        return new WXSearchIndexVO(wxSearchIndexDefaultKeywordVO,wxSearchIndexDefaultKeywordVOS,wxSearchHistoryVOS);
    }


    // 根据userId删除历史搜索记录，逻辑删除
    @Override
    public int clearHistory(int id) {
        MarketSearchHistory marketSearchHistory = new MarketSearchHistory();
        marketSearchHistory.setUserId(id);
        marketSearchHistory.setDeleted(Constant.NOEXIST);
        marketSearchHistory.setUpdateTime(new Date());
        // 条件设置为没有逻辑删除的，userId等于登录者
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria historyExampleCriteria = marketSearchHistoryExample.createCriteria();
        historyExampleCriteria.andDeletedEqualTo(Constant.EXIST);
        historyExampleCriteria.andUserIdEqualTo(id);
        int i = historyMapper.updateByExampleSelective(marketSearchHistory, marketSearchHistoryExample);
        return i;// 返回影响行数
    }

    @Override
    public List<String> helper(String keyword) {

        return keywordMapper.selectSearchKeyWord(keyword.trim());


    }
}
